﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PloobsEngine.Material;
using PloobsEngine;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using PloobsEngine.Modelo;
using Etapa1.Import;
using Etapa1.Import.Anim;
using Microsoft.Xna.Framework.Input;
using PloobsEngine.Utils;

namespace Etapa1.BezierExperiments
{
    public class ImportAnimationShader : IShader
    {
        public ImportAnimationShader(FillMode fillMode , Bone root)
        {
            this.fillMode = fillMode;
            this.root = root;
            
            Bone.BoneCount(root,ref boneNumber);
            BoneMatrix = new Matrix[boneNumber];
        }

        int boneNumber = 0;
        Bone root;
        FillMode fillMode = FillMode.Solid;        
        Effect _shader;
        Matrix[] BoneMatrix;


        Matrix m = Matrix.Identity;
        public override void Update(PloobsEngine.SceneControl.IObject ent, IList<PloobsEngine.Light.ILight> lights)
        {            
            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {   
                m *= Matrix.CreateTranslation(Vector3.UnitY * 10);
                root.Childs[0].Childs[0].InternalLocalOffset = m;                
            }            

            root.Update(Matrix.Identity, ref BoneMatrix);
            
            
            base.Update(ent, lights);
        }

        Dictionary<String, Bone> map = new Dictionary<string, Bone>();
        public override void Initialize()
        {
            Bone.GetNameBoneMapping(root, ref map);
            _shader = EngineStuff.CustomContentManager.GetAsset<Effect>("Effects/Import");         
            base.Initialize();
        }

        public override void Draw(IModelo modelo, PloobsEngine.SceneControl.IRenderHelper render, PloobsEngine.Cameras.ICamera cam)
        {
            AnimatedModel vbm = modelo as AnimatedModel;
            
            _shader.Parameters["View"].SetValue(cam.View);
            _shader.Parameters["Bones"].SetValue(BoneMatrix);
            //_shader.Parameters["Texture"].SetValue(vbm.Texture);
            _shader.Parameters["Projection"].SetValue(cam.Projection);
            Matrix[] boneTransforms = modelo.getBonesTransformation();
            Matrix wld = obj.getWorldMatrix();
            EngineStuff.GraphicsDevice.RenderState.FillMode = fillMode;
            for (int i = 0; i < modelo.MeshNumber; i++)
            {
                BatchInformation[] bi = modelo.GetBatchInformation(i);
                Matrix w1 = Matrix.Multiply(wld, boneTransforms[modelo.GetParentBoneIndex(i)]);
                this._shader.Parameters["World"].SetValue(w1);
                for (int j = 0; j < bi.Count(); j++)
                {

                    this._shader.Begin();
                    EngineStuff.GraphicsDevice.VertexDeclaration = bi[j].VertexDeclaration;
                    EngineStuff.GraphicsDevice.Vertices[0].SetSource(modelo.GetVertexBuffer(i), bi[j].StreamOffset, bi[j].VertexStride);
                    EngineStuff.GraphicsDevice.Indices = modelo.GetIndexBuffer(i);

                    this._shader.CurrentTechnique.Passes[0].Begin();
                    EngineStuff.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, bi[j].BaseVertex, 0, bi[j].NumVertices, bi[j].StartIndex, bi[j].PrimitiveCount);
                    this._shader.CurrentTechnique.Passes[0].End();
                    this._shader.End();
                }

            }
            EngineStuff.GraphicsDevice.RenderState.FillMode = FillMode.Solid;
            
        }
        public override MaterialType MaterialType
        {
            get { return MaterialType.DEFERRED; }
        }

    }
}
